package com.course.android.activity.base;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.baselib.android.utils.Logger;
import com.baselib.android.utils.ToastTool;
import com.course.android.R;
import com.course.controller.permission.PermissionGranteCallback;
import com.course.dic.ActivityIntentKey;

public abstract class BaseActivity extends AppCompatActivity implements PermissionGranteCallback {
    ActivityResultLauncher<Intent> intentLauncher ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(bindLayout());
        intentLauncher =  registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            //此处是跳转的result回调方法
            if (result.getData() != null && result.getResultCode() == Activity.RESULT_OK) {
                onActivityResultCallBack(result.getData().getIntExtra(ActivityIntentKey.REQ_CODE,0),result.getData());
            }
        });

        initView();
        initData();
        Logger.e(getClass().getName() + "onCreate");
    }

    protected ActivityResultLauncher<Intent> newIntentLauncher(){
        return intentLauncher;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        Logger.e(getClass().getName() + "onDestroy");
    }

    /**
     * 实例化view
     */
    protected abstract void initView();

    /**
     * 实例化数据
     */
    protected abstract void initData();

    protected abstract int bindLayout();
    protected abstract void onActivityResultCallBack(Integer reqCode,Intent data);

    /**
     * 系统提醒
     *
     * @param resId
     */
    protected void showToast(int resId) {
        ToastTool.show(this, resId);
    }

    private boolean granted(int[] grantResults) {
        if (grantResults == null || grantResults.length == 0) {
            return true;
        }
        for (int i : grantResults) {
            if (i != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void granted(String[] permissions, int reqCode) {

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (granted(grantResults)) {
            granted(permissions, requestCode);
        } else {

            new AlertDialog.Builder(this)
                    .setTitle("警告！")
                    .setMessage("请前往设置->应用->" + getResources().getString(R.string.app_name) + "->权限中打开相关权限，否则功能无法正常运行！")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            try {
                                Intent intent = new Intent();
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                if (Build.VERSION.SDK_INT >= 9) {
                                    intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                                    intent.setData(Uri.fromParts("package", getApplication().getPackageName(), null));
                                } else if (Build.VERSION.SDK_INT <= 8) {
                                    intent.setAction(Intent.ACTION_VIEW);
                                    intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
                                    intent.putExtra("com.android.settings.ApplicationPkgName", getApplication().getPackageName());
                                }
                                startActivity(intent);
                            } catch (Exception e) {
                            }
                        }
                    }).show();
        }
        return;
    }

    protected void grantPermission(int requestCode) {

    }

}
